#include "StdAfx.h"
#include "WeatherView.h"
#include "../kernel.h"
#include <math.h>

extern Kernel* g_kernel;

#define TIME g_kernel->GetTime()

WeatherView::WeatherView(void)
{
    m_weather = NULL;

    m_sThunder1 = g_kernel->GetResource()->LoadSound( "data\\sounds\\weather\\thunder.wav");
    m_sRain = g_kernel->GetResource()->LoadSound( "data\\sounds\\weather\\rain01.wav");
    m_iRain1[0] = g_kernel->GetResource()->LoadSingleImage( "misc/rain1.tga");
    m_iRain1[1] = g_kernel->GetResource()->LoadSingleImage( "misc/rain2.tga");
    m_iRain1[2] = g_kernel->GetResource()->LoadSingleImage( "misc/rain3.tga");
    m_iRain1[3] = g_kernel->GetResource()->LoadSingleImage( "misc/rain4.tga");
    m_rainAnim.Set( 100, 1, 1, 4 );
          
    
}


/** Update the weather effects */
void WeatherView::Update(long dt)
{
    m_rainAnim.Update( dt );
}
/** Render the weather effects */
void WeatherView::Render( IRender *r, ICamera* cam )
{
    if ( m_weather->IsFogEnabled() )
    {
        r->EnableFog();
        r->ChangeFogColor( m_weather->GetFogColor() );
    }

    if ( m_weather->IsRaining() )
    {
        m_sRain->Loop();

        m_rainAnim.Update( g_kernel->GetFrameTime() );
        int w = g_kernel->GetCvars()->GetFloatValue("v_width");
        int h = g_kernel->GetCvars()->GetFloatValue("v_height");
        r->DrawImage( &m_iRain1[m_rainAnim.GetFrame()-1], 0, 0, w, h );
    } 
    else
    {
        m_sRain->Stop();
    }
    /**
     * Create lightning if needed
     */
    switch(m_weather->CheckLightning())
    {
        case LT_SMALL:
        case LT_MED:
        case LT_BIG:
            Randomize(r);
            m_sThunder1->Play(false);
            break;
        default: // just have the normal nightfall
            //r->SetColorEffect( 0.20f, 0.19f, 0.29f );
            r->SetColorEffect( 1/(100/m_weather->GetTimeOfDay()),1/(100/m_weather->GetTimeOfDay()),1/(110/m_weather->GetTimeOfDay()) );
    }


}

/** Randomize Lightning Effect */
void WeatherView::Randomize(IRender *r)
{
    int ran = rand()/10000;
    switch( ran )
    {
    case 0:
        r->SetColorEffect( 0.79f, 0.70f, 0.75f );
        break;
    case 1:
        r->SetColorEffect( 0.92f, 0.91f, 0.90f );
        break;
    default:
        r->SetColorEffect( 1, 1, 1 );
    }
}

WeatherView::~WeatherView(void)
{
}
